package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;

import com.shop.cloud.domain.hongfu.Message;
import com.shop.cloud.domain.hongfu.StoreApplication;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.dao.hongfu.StoreApplicationDao;
import com.shop.cloud.service.hongfu.IStoreApplicationService;
import com.shop.cloud.util.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.JPush.MsgPushUtils;
import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;

/**
 * 
 */
@Service("storeApplicationService")
public class StoreApplicationService extends BaseService<StoreApplication> implements IStoreApplicationService{
    
    @Autowired
	private StoreApplicationDao storeApplicationDao;
    
    @Autowired
    private MessageService messageService;
    
    @Autowired
    private UserService userService;

	@Override
	public MybatisDao<StoreApplication> getEntityDao() {
		return storeApplicationDao;
	}

	@Override
	public int HandleAddStore(Long userId, StoreApplication storeApplication) {
		// TODO Auto-generated method stub
		
		//查询申请条数
		StoreApplication storeApplication2 = new StoreApplication();
		storeApplication2.setUserId(userId);
		long count = storeApplicationDao.countByEntity(storeApplication2);
		
		storeApplication.setUserId(userId);
		storeApplication.setCreateTime(new Timestamp((new Date()).getTime()));
		storeApplication.setStatus(0);
		int flag = 0;
		if (count > 2) {
			return 2;
		}else {
			
			flag = storeApplicationDao.insert(storeApplication);
			if (flag > 0) {
				//生成消息
				Message message = new Message();
				message.setUserId(userId);
				message.setType(1);
				message.setPushType(1);
				message.setContent("你的加盟信息已提交成功，请耐心等待官方工作人员联系！");
				message.setCreateTime(new Timestamp(new Date().getTime()));
				message.setTitle("门店加盟");
				message.setRemark("门店加盟");
				//推送消息
				messageService.insert(message);
				
				//极光推送
				try {
					MsgPushUtils.addJPush(message.getContent(), userId.toString());
				} catch (APIConnectionException e) {
					e.printStackTrace();
				} catch (APIRequestException e) {
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	@Override
	public Page<StoreApplication> handlePage(StoreApplication storeApplication) {
		// TODO Auto-generated method stub
		storeApplication.setPageSize((storeApplication.getPageSize_()==null?20:storeApplication.getPageSize_()));
		long count = getEntityDao().countByEntity(storeApplication);
		Page<StoreApplication> page = new Page<StoreApplication>(storeApplication.getPageIndex_(), storeApplication.getPageSize_());
		List<StoreApplication> list = storeApplicationDao.findByEntity(storeApplication);
		for (StoreApplication storeApplication2 : list) {
			User user = userService.get(storeApplication2.getUserId());
			if(user!=null){
				storeApplication2.setNickName(user.getNickname());
			}else{
				storeApplication2.setNickName("");
			}
			
		}
		page.setData(list);
		page.setTotalCount(count);
		return page;
	}
    
}